﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyFrameWork.Dam;
using MvcApplication5.Models;
using System.Data;
using System.Data.Common;
using MvcApplication5.Common;

namespace MvcApplication5.DAM
{
    public class PositionDAM : DataDAM
    {
        /// <summary>
        /// 创建连接数据库
        /// </summary>
        public PositionDAM()
        {
            this.CreateDataBase("MAINDB");
        }
        /// <summary>
        /// 新规职位
        /// </summary>
        /// <param name="model">职位model</param>
        /// <returns>受影响行数</returns>
        public int SavePositionInfo(PositionModel model)
        {
            try
            {
                return this.InsertData("TB_position", "position_id", model, null);
            }
            catch
            {
                return -1;
            }
        }
        /// <summary>
        /// 删除职位
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DeletePositionInfo(string position_id)
        {
            //递归找到与position_id管理的子数据或子数据的子数据
            string sql = string.Format(@"
                                       WITH position AS
                                       (
	                                     SELECT * FROM TB_position p1 WHERE p1.position_id = {0}
	                                     UNION ALL
	                                     SELECT p1.* FROM TB_position p1
                                         JOIN position p2 ON p1.parent_id = p2.position_id
                                       )
                                       UPDATE 
                                            TB_position
                                       SET
                                            record_status = '1',update_user=@update_user, update_date=@update_date
                                       WHERE 
                                           (record_status = '0' AND  position_id IN (SELECT position_id FROM position) ) ", position_id);

            DbCommand command = GetSqlStringCommand(sql);

            AddInParameter(command, "@update_user", DbType.String, AppContext.Current.LoginUser.employee_id);
            AddInParameter(command, "@update_date", DbType.DateTime, DateTime.Now);

            return ExecuteNonQuery(command, null);
        }
        /// <summary>
        /// 获取职位表中的相应id的数据
        /// </summary>
        /// <param name="position_id"></param>
        /// <returns></returns>
        public DataSet GetPositionInfo(string position_id)
        {
            try
            {
                ConditionList conditionList = new ConditionList(ConditionList.ConditionType.AND);

                conditionList.Add("position_id", ConditionList.EquipType.EQUIP, position_id);

                conditionList.Add("record_status", ConditionList.EquipType.EQUIP, "0");

                return this.SelectData("TB_position", "*", conditionList, null);
            }
            catch (Exception ex)
            {
                return null;
            }

        }
        /// <summary>
        /// 修改职位信息
        /// </summary>
        /// <param name="model">职位model</param>
        /// <returns>受影响行数</returns>
        public int UpdatePositionInfo(PositionModel model)
        {
            try
            {
                //this.SetRejectList(UserModel.rejectColumn);

                ConditionList conditionList = new ConditionList(ConditionList.ConditionType.AND);

                conditionList.Add("position_id", ConditionList.EquipType.EQUIP, model.position_id.ToString());

                Dictionary<string, string> targetList = new Dictionary<string, string>();

                targetList.Add("position_name", model.position_name.ToString());

                return this.UpdateData("TB_position", targetList, conditionList, null);
            }
            catch (Exception)
            {

                return -1;
            }
        }

    }
}